package com.sh.demo.qa.util;

import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.sh.demo.qa.controller.IndexController;

public class PageUtil {

	protected final static Logger logger = LoggerFactory.getLogger(IndexController.class);

	/**
	 * 获得工具条（JS的可以参照着写）
	 * 使用隐藏域的方式避免拼凑的计算，还有get、混合get/post会发生过滤器问题
	 */
	public static String getPageBar(String path, Map params, Page pageBean){
		// page信息
		int currentPage = pageBean.getNumber(); // 0页开始
		int totalPage = pageBean.getTotalPages();
		int size = pageBean.getSize();
		long totalCount = pageBean.getTotalElements();
		// 拼接字符串
		StringBuilder bar = new StringBuilder();
		// 最好不要让form提交地址拼接参数的方式
		bar.append("<form method='post' id='pageform' name='pageform' action='' style='margin-bottom: 0px;'>");
		// 最好不要让form提交地址拼接参数的方式
		bar.append(params2Input(params, pageBean));
		// 链接
		if(hasFirstPage(pageBean)){
			int firstPage = 0;
			bar.append("<a href='javascript:changePage(" + firstPage + ");'>首页</a>&nbsp;");
		}
		if(hasPreviousPage(pageBean)){
			int prePage = currentPage - 1;
			bar.append("<a href='javascript:changePage(" + prePage + ");'>上一页</a>&nbsp;");
		}
		if(hasNextPage(pageBean)){
			int nextPage = currentPage + 1;
			bar.append("<a href='javascript:changePage(" + nextPage + ");'>下一页</a>&nbsp;");
		}
		if(hasLastPage(pageBean)){
			int lastPage = totalPage - 1;
			bar.append("<a href='javascript:changePage(" + lastPage + ");'>末页</a>&nbsp;");
		}
		// 页数
		bar.append("当前第" + (currentPage + 1) + "页&nbsp;&nbsp;");
		bar.append("每页" + size + "条&nbsp;&nbsp;");
		bar.append("共" + totalPage + "页&nbsp;&nbsp;");
		bar.append("共" + totalCount + "条&nbsp;&nbsp;");
		
		// 脚本
		bar.append("<script language='javascript'>");
		bar.append("function changePage(page){");
//		bar.append("document.getElementById('page_page').value=page;");
//		bar.append("alert(document.getElementById('page_page').value);");
//		bar.append("document.pageform.action = '" + path + "';");
		bar.append("document.pageform.action = '" + path + "?page=' + page + '&page.size=' + " + size + ";");
		bar.append("document.pageform.submit();");
		bar.append("}");
		bar.append("</script>");
		bar.append("</form>");
		return bar.toString();
	}

	public static String params2Input(Map params, Page pageBean) {
		StringBuffer sb = new StringBuffer();
		for (Entry param : (Set<Entry>) params.entrySet()) {
			String paramName = (String) param.getKey();
			Object paramValue = param.getValue();
			if(paramValue == null || paramName.equals("class") || paramName.equals("page") || paramName.equals("page.size") || paramName.equals("page.sort") || paramName.equals("page.sort.dir")){
				continue;
			}
			sb.append("<input type='hidden' id='page_" + paramName + "' name='" + paramName + "' value='" + (paramValue == null ? "" : paramValue.toString()) + "' />");
		}
//		sb.append("<input type='hidden' id='page_page' name='page' value='" + pageBean.getNumber() + "' />");
//		sb.append("<input type='hidden' id='page_page.size' name='page.size' value='" + pageBean.getSize() + "' />");
		// TODO: 如何实现多个的
//		sb.append("<input type='hidden' name='page.sort' value='" + pageable.getSort(). + "' />");
//		sb.append("<input type='hidden' name='page.sort.dir' value='" + pageable.getPageNumber() + "' />");
		return sb.toString();
	}
	
	/**
	 * 解析请求地址，只留关键参数
	 */
	public static String parseURL(String path, Page pageBean){
		StringBuffer sb = new StringBuffer();
		sb.append("page=" + pageBean.getNumber());
		sb.append("&page.size=" + pageBean.getSize());
		return path + "?" + sb.toString();
	}
	
	 public static boolean hasPreviousPage(Page pageBean) {
		 int currentPage = pageBean.getNumber();
		 return (currentPage > 0);
	 }
	 
	 public static boolean hasNextPage(Page pageBean) {
		 int currentPage = pageBean.getNumber();
		 int totalPage = pageBean.getTotalPages();
		 return (currentPage < totalPage-1);
	 }
	 
	 public static boolean hasFirstPage(Page pageBean) {
		 int currentPage = pageBean.getNumber();
		 // 如果就一页的话，就不链接了
		 return (currentPage > 0);
	 }
	 
	 public static boolean hasLastPage(Page pageBean) {
		 int currentPage = pageBean.getNumber();
		 int totalPage = pageBean.getTotalPages();
		 // 防止currentPage=0,totalPage=0
		 return (totalPage > 0 && currentPage < totalPage-1);
	 }
}
